import { queryDetail } from '../services';
import { queryProcess } from '../../services';
import { formDataFormat } from '../../utils';

export default {
    namespace: 'flowOldProcess',
    state: {
        detailData: {},
        processList: []
    },

    effects: {
        *fetchDetail({ payload, callback }, { call, put }) {
            const response = yield call(queryDetail, payload);
            if (response && response.code === 10000) {
                const returnData = response.data;
                // 把返回的表单值和表单做处理
                const newData = formDataFormat(returnData);

                yield put({
                    type: 'save',
                    payload: {
                        detailData: { ...returnData, formList: newData }
                    }
                });
                if (callback) {
                    callback(response);
                }
            }
        },
        *fetchProcess({ payload, callback }, { call, put }) {
            const response = yield call(queryProcess, payload);
            yield put({
                type: 'save',
                payload: {
                    processList: response.data
                }
            });
            if (callback) {
                callback(response);
            }
        }
    },
    reducers: {
        clearDetail(state) {
            return {
                ...state,
                detailData: {},
                processList: []
            };
        },
        save(state, { payload }) {
            console.log('payload', payload);
            return {
                ...state,
                ...payload
            };
        }
    }
};
